package com.cxs.xiaomi.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

/*
 * @Author:cxs
 * @Date:2021/9/14  16:28
 * @Version 1.0
 * @Motto:放下杂念,只为迎接明天更好的自己
 * */
public class StringUtil {
    // 判空
    public static boolean empty(String str){
        return str == null || str.trim().length() == 0;
    }
    // 随机生成20位检验码
    public static String generateCode(){
        return UUID.randomUUID()
                .toString()
                .replaceAll("-","")
                .substring(0,20);
    }

    // 生成唯一的32位字符串
    public static String generateUniqueKey(int num){
        SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMddHHmmss");
        return randomStr(num - 15) + "_" + sdf.format(new Date());
    }

    private static String randomStr(int num){
        String result = "";
        Random random = new Random();
        for (int i = 0; i < num; i++) {
            result += random.nextInt(9);
        }
        return result;
    }
}
